# -*- coding: utf-8 -*-
def readp():
    f = open("prime.txt")
    l = f.readlines()
    f.close

    result = []
    for stra in l:
        a = stra.split(' ')
        result = map(int,a)
    return result

def readp50000():
    f = open("prime10000.txt")
    l = f.readlines()
    f.close

    result = []
    for stra in l:
        a = stra.split(' ')
        result = map(int,a)
    return result

primes = readp()

primes5000 = readp50000()
sums = []

def panduanab(a,b):
    stra = str(a)
    strb = str(b)
    strf = stra + strb
    if len(strf) > 8 : return 0
    if int(stra+strb) not in primes:
##            print u"不在素数范围",a,b
            return 0
    if int(strb+stra) not in primes:
##            print u"不在素数范围",a,b
            return 0
    return 1

def panduanabc(a,b,c):
    stra = str(a)
    strb = str(b)
    strc = str(c)

    strf = stra + strc
    if len(strf) > 8 : return 0
    strf = strb + strc
    if len(strf) > 8 : return 0

    if int(stra+strc) not in primes:
            return 0
    if int(strc+stra) not in primes:
        return 0

    if int(strb+strc) not in primes:
        return 0
    if int(strc+strb) not in primes:
        return 0
    return 1

def panduanabcd(a,b,c,d):
    stra = str(a)
    strb = str(b)
    strc = str(c)
    strd = str(d)
    strf = stra + strd
    if len(strf) > 8 : return 0
    strf = strb + strd
    if len(strf) > 8 : return 0
    strf = strc + strd
    if len(strf) > 8 : return 0

    if int(stra+strd) not in primes:
            return 0
    if int(strb+strd) not in primes:
            return 0
    if int(strc+strd) not in primes:
            return 0
    if int(strd+stra) not in primes:
            return 0
    if int(strd+strb) not in primes:
            return 0
    if int(strd+strc) not in primes:
            return 0

    return 1

def panduanabcde(a,b,c,d,e):
    stra = str(a)
    strb = str(b)
    strc = str(c)
    strd = str(d)
    stre = str(e)
    strf = stra + stre
    if len(strf) > 8 : return 0
    strf = strb + stre
    if len(strf) > 8 : return 0
    strf = strc + stre
    if len(strf) > 8 : return 0
    strf = strd + stre
    if len(strf) > 8 : return 0

    if int(stra+stre) not in primes:
            return 0
    if int(strb+stre) not in primes:
            return 0
    if int(strc+stre) not in primes:
            return 0
    if int(strd+stre) not in primes:
            return 0

    if int(stre+stra) not in primes:
            return 0
    if int(stre+strb) not in primes:
            return 0
    if int(stre+strc) not in primes:
            return 0
    if int(stre+strd) not in primes:
            return 0

    return 1

def main():
    plen = len(primes5000)

    for a in xrange(1,plen):
        starb = a + 1
        for b in xrange(starb,plen):
            if panduanab(primes[a],primes[b]):
##                print primes[a],primes[b]
                starc = b + 1
                for c in xrange(starc,plen):
                    if panduanabc(primes[a],primes[b],primes[c]):
##                        print primes[a],primes[b],primes[c]
                        stard = c + 1
                        for d in xrange(stard,plen):
                            if panduanabcd(primes[a],primes[b],primes[c],primes[d]):
                                pls = [primes[a],primes[b],primes[c],primes[d]]
                                print primes[a],primes[b],primes[c],primes[d],sum(pls)
##                                exit()
                                stare = d + 1
                                for e in xrange(stare,plen):
                                    if panduanabcde(primes[a],primes[b],primes[c],primes[d],primes[e]):
                                        pls = [primes[a],primes[b],primes[c],primes[d],primes[e]]
                                        print u'五个:',primes[a],primes[b],primes[c],primes[d],primes[e],sum(pls)
##                                        exit()

if __name__ == '__main__':
    import time
    start = time.time()
    main()
    print('Execution time: {0} seconds'.format(time.time() - start))

##13 5197 5701 6733 8389
##26033

##3 7 109 673 792
##3 7 541 4159 4710
##3 11 701 8747 9462
##3 17 449 2069 2538
##3 17 449 6353 6822
##3 17 449 6599 7068
##3 37 67 2377 2484
##3 37 67 5923 6030
##3 331 739 8431 9504
##3 467 617 4253 5340
